package com.wuseguang.springcloud.util;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TaskCpstableUtils{

    private static Logger logger = LoggerFactory.getLogger(TaskCpstableUtils.class);

    public static final String getui_appId = "ypayor5Hqw71T5vEUxJZS2";
    public static final String getui_appKey = "Y2Qz3tt9V47yBMnNbNns7A";
    public static final String getui_masterSecret = "EraLoCYD9t8FaA96KOLqL1";

   /* private Map<String, Object> map = new HashMap<String, Object>();
    private List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();

    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    public List<Map<String, Object>> getMaps() {
        return maps;
    }

    public void setMaps(List<Map<String, Object>> maps) {
        this.maps = maps;
    }*/

    /*@Override
    public void run() {
        try {
            logger.info("scheduled()-->Thread.currentThread().getName()-->{}", Thread.currentThread().getName());
            if (map != null && map.size() > 0) {
                String s = sendandroid((String) map.get("clientid"), map.toString());
                logger.info("sendandroid==>{}", s);
            }
            logger.info("List<Map<String, Object>> maps====>");
            if (maps != null && maps.size() > 0) {
                logger.info("List<Map<String, Object>> maps====>{}", maps.toString());
                for (int i = 0; i < maps.size(); i++) {
                    Map<String, Object> stringObjectMap = maps.get(i);
                    if (stringObjectMap != null && stringObjectMap.get("clientid") != null && !"".equals(stringObjectMap.get("clientid"))) {
                        String s = sendandroid((String) stringObjectMap.get("clientid"), stringObjectMap.toString());
                        logger.info("maps==>{}", s);
                    }
                }
            }
        } catch (Exception e) {
            logger.info("scheduled()-->error={}", e);
        }
    }*/


    public static String sendandroid(String cid, String content) {
        if (cid != null && !"".equals(cid)) {
            logger.info("sendandroid(), cid=" + cid + ", content=" + content);
            // 配置返回每个用户返回用户状态，可选
            System.setProperty("gexin.rp.sdk.pushlist.needDetails", "true");
            logger.info("设置了个推属性");
            // IGtPush push = new IGtPush(host, appKey, masterSecret);
            IGtPush push = new IGtPush(getui_appKey, getui_masterSecret, true);
            // 代理商版本
            logger.info("准备个推推送模板.........");
            // 通知透传模板
            TransmissionTemplate template = transmissionTemplateDemo1(content);
            logger.info("个推模板已构建........");
            SingleMessage message = new SingleMessage();
            message.setData(template);
            // 设置消息离线，并设置离线时间
            message.setOffline(true);
            message.setPushNetWorkType(0);// 1：wifi ，0：不限，默认不限
            // 离线有效时间，单位为毫秒，可选
            message.setOfflineExpireTime(24 * 1000 * 3600);
            logger.info("班牌推送消息已构建...............");
            // 配置推送目标
            Target target = new Target();
            target.setAppId(getui_appId);
            logger.info("setAppid finished...............");
            target.setClientId(cid);// dd236f5e2ec9c2e95c9e2915351557d0 467d31925d16fddb52b22bb20a009267
            IPushResult ret = null;
            try {
                logger.info("班牌开始推送，android");
                ret = push.pushMessageToSingle(message, target);
                logger.info("班牌推送完毕，android");
            } catch (RequestException e) {
                logger.info("班牌推送失败，android");
                logger.error("e={}", e);
                ret = push.pushMessageToSingle(message, target, e.getRequestId());
                logger.info("二次班牌推送完成，android");
            }
            logger.info("ret.tostring()={}", ret.toString());
            if (ret != null && ret.getResponse() != null) {
                logger.info("推送响应结果.........." + ret.getResponse().get("result").toString());
                return ret.getResponse().toString();
            } else {
                logger.info("服务器响应异常.....");
                return "NO";
            }
        } else {
            logger.info("未发送，clientid=" + cid);
            return "NO";
        }
    }

    @SuppressWarnings("deprecation")
    public static TransmissionTemplate transmissionTemplateDemo1(String content) {
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(getui_appId);
        template.setAppkey(getui_appKey);
        // 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
        template.setTransmissionType(2);
        template.setTransmissionContent(content);
        // 设置定时展示时间
        // template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00");
        return template;
    }

    public static void main(String[] args) {
        String sendandroid = sendandroid("6b1914d4301d0a40bd7646cca41e5474", "shutdown=1, restartflag=0, volumeflag=0, screenflag=1, holidayheartbeat=1, upgradeflag=0, picturesflag=0, videoflag=0, clientid=6b1914d4301d0a40bd7646cca41e5474, classflag=0, dialogflag=0, devicetypeflag=0");
        System.out.println(sendandroid);
    }
}
